using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

using Common;

using GrpcClient.Models;
using GrpcClient.UI;

namespace GrpcClient.Managers
{
    public class TabManager : Singleton<TabManager>
    {
        private string defaultUrl = "https://www.baidu.com";

        private IDictionary<string, WebViewControl> Tabs = new Dictionary<string, WebViewControl>();

        public event Action<WebViewControl>? AddTabEvent;
        public event Action<WebViewControl, int>? InsertTabEvent;
        public event Action<WebViewControl>? RemoveTabEvent;
        public event Action<string>? UpdateInputUrlEvent;

        public int Selected { get; set; }


        public TabManager()
        {
            if (ConfigContainer.Instance.cacheManager.Get("mainpage", out string du))
            {
                defaultUrl = du;
            }
            else
            {
                defaultUrl = ResourceManager.Instance.MainPage;
            }
        }


        public void Init()
        {
            Create(defaultUrl);
        }

        public WebViewControl? Get(string id)
        {
            if (Tabs.ContainsKey(id)) return Tabs[id];
            return null;
        }

        public List<WebViewControl> GetAll()
        {
            return Tabs.Values.ToList();
        }

        public WebViewControl Create(string url = "")
        {
            WebViewControl webview = new WebViewControl();

            if (!string.IsNullOrEmpty(url))
            {
                webview.webView.Source = new Uri(url);
                UpdateInputUrl(url);
            }

            Tabs.Add(webview.Id, webview);
            if (AddTabEvent != null)
            {
                AddTabEvent.Invoke(webview);
            }

            return webview;
        }

        public void Remove(string id)
        {
            if (Tabs.ContainsKey(id))
            {
                var webview = Tabs[id];
                Tabs.Remove(id);
                if (RemoveTabEvent != null)
                {
                    RemoveTabEvent.Invoke(webview);
                }

                if (Tabs.Count == 0)
                {
                    Init();
                }
            }
        }

        public void UpdateInputUrl(string url)
        {
            if (UpdateInputUrlEvent != null)
                UpdateInputUrlEvent.Invoke(url);
        }

        public static string FormatUrl(string uri)
        {
            string pattern = @"^(((ht|f)tps?):\/\/)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}\/?";
            string localPattern = @"^(https?:\/\/)?(localhost|127.0.0.1)?(:\d{1,5})?$";

            if (Regex.IsMatch(uri, pattern))
            {
                if (!uri.StartsWith("http://") && !uri.StartsWith("https://") && !uri.StartsWith("file:///") && !uri.StartsWith("ftp://") && !uri.StartsWith("ftps://"))
                {
                    if (!uri.StartsWith("www."))
                    {
                        uri = "www." + uri;
                    }
                    uri = "https://" + uri;
                }
            }
            else if (Regex.IsMatch(uri, localPattern))
            {
                if (uri.Split(':').Length == 1)
                {
                    uri = uri + ":8080";
                }
            }
            else
            {
                uri = "https://www.baidu.com/s?ie=utf-8&wd=" + uri;
            }

            //if (!uri.StartsWith("http://") && !uri.StartsWith("https://"))
            //{
            //    if (!uri.StartsWith("www."))
            //    {
            //        uri = "www." + uri;
            //    }
            //    uri = "https://" + uri;
            //}
            return uri;
        }
    }
}
